import os,re,subprocess
import sys
import setting
from pytube import YouTube
from threading import Thread
import subprocess
import time
import to_mp3
import tkinter
import win

def get_app_path():
    """获取打包后的可执行文件所在路径"""
    if getattr(sys, 'frozen', False):
        return os.path.dirname(sys.executable)
    else:
        return os.path.dirname(os.path.abspath(__file__))
    

BATH_PATH=get_app_path()
sys.path.append(BATH_PATH)


class YTB():
    # link = input('请输入YouTube视频链接：').strip()
    # yt_name = YouTube(link).title # 获取视频名（不带后缀格式名）
    # yt = YouTube(link).streams

    # def youtube_v():
    #     if not os.path.exists(setting.VIDEO_PATH) : #判断下载目录是否存在
    #         os.mkdir(setting.VIDEO_PATH)
    #     title_v=yt.get_by_itag(137).download(setting.VIDEO_PATH).title()  #下载并返回视频名（带后缀格式名）
    #     # 下载之前先看视频源有哪几种清晰度 1080p 选137 | 1080p 60fps 选299
    #     return title_v

    def __init__(self):
        self.yt_name = ''
        self.file_path = ''
    def youtube_a(self,w):
        try:
            if not os.path.exists(setting.AUDIO_PATH) :  #判断下载目录是否存在
                os.mkdir(setting.AUDIO_PATH)
            link = w.url_entry.get().strip()
            self.yt_name = YouTube(link).title # 获取视频名（不带后缀格式名）
            yt = YouTube(link).streams
            print("get:"+link)
            title_a=yt.get_by_itag(251).download(setting.AUDIO_PATH).title()
            self.file_path = title_a
            # 音频251默认160kbps,webm格式
            print('音频下载成功')
            return title_a
        except:
            print("下载失败请检查网络")
            w.downloading_win.destroy
            w.modal("下载失败请检查网络,并重启软件再次尝试下载!")
    # def video_add_mp4(video, audio, outer_path):
    #     order = f'ffmpeg -i "{video}" -i "{audio}" -c:v copy -c:a aac -strict -2 "{outer_path}"' # 拼接系统命令用于执行ffmpeg程序
    #     print(order)
    #     os.system(order)
    #     subprocess.Popen(order, shell=True)  # 执行ffmpeg
    #     subprocess.Popen('explorer %s'%setting.OUTER_PATH, shell=True) # 打开合成目录
    #     subprocess.Popen('explorer %s'%c, shell=True)   # 打开合成后的视频


if __name__ == '__main__':
    # t = Thread(target=youtube_v)
    # t2 = Thread(target=youtube_a)
    # # t.start()
    # t2.start()
    # t2.join()
    # print('音频下载成功')
    # time.sleep(10)

    main_win = win.WIN()

    # win.root.mainloop()
    # t2 = Thread(target=youtube_a)
    # t2.start()
    # t2.join()
    # print('音频下载成功')
    # print(youtube_a())
    # time.sleep(1)
    # input_file = youtube_a()
    # output_file = input_file.replace('Webm','mp3')
    # print(output_file)
    # if os.path.exists(input_file):
    #     print("Input file exists!")
    # else:
    #     print("Input file does not exist!")
    # to_wav.webm_to_mp3(input_file, output_file)   


    # t.join()
    # print('视频下载成功')
    # a = youtube_v() # 视频
    # b = youtube_a()  # 音频
    # outer_name=f'{yt_name}.mp4'
    # re_c=re.sub(r'[/*?"<>|]','',outer_name ) # 过滤文件名中不允许出现的几个字符
    # c = os.path.join(setting.OUTER_PATH, re_c)
    # video_add_mp4(a,b,c)